package dailytest;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Base64;

/**
 * @author: heweiwei
 * @Date: 2023/2/21 14:37
 * @Desc:
 */
public class WardListRequest {

    public static void main(String[] args) {
        // 输入文件路径（包含Base64编码的PDF内容）
        String inputFilePath = "/Users/heweiwei/base64.txt";
        // 输出文件路径（生成的PDF文件）
        String outputFilePath = "/Users/heweiwei/base64.pdf";
        try {
            // 从文件中读取Base64编码的字符串
            String base64EncodedString = readBase64FromFile(inputFilePath);

            // 解码Base64字符串
            byte[] pdfBytes = Base64.getDecoder().decode(base64EncodedString);

            // 将解码后的字节数组写入PDF文件
            Files.write(Paths.get(outputFilePath), pdfBytes);
            System.out.println("PDF file has been created successfully at: " + outputFilePath);
        } catch (IOException e) {
            e.printStackTrace();
            System.err.println("Error occurred while processing the files.");
        }
    }

    /**
     * 从文件中读取Base64编码的字符串
     *
     * @param filePath 文件路径
     * @return Base64编码的字符串
     * @throws IOException 如果读取文件时发生错误
     */
    private static String readBase64FromFile(String filePath) throws IOException {
        StringBuilder contentBuilder = new StringBuilder();
        try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
            String currentLine;
            while ((currentLine = br.readLine()) != null) {
                contentBuilder.append(currentLine);
            }
        }
        return contentBuilder.toString();
    }


}
